package com.hllxd.hism.star.basic.service.service.rock;

import com.hllxd.hism.star.basic.service.beans.BillingOwnerPageBO;
import com.hllxd.hism.star.basic.service.collect.NumberUtils;
import com.hllxd.hism.star.basic.service.dao.mapper.doris.BillingOwnerMapper;
import com.hllxd.hism.star.basic.service.model.doris.BillingOwnerEntity;
import com.hllxd.hism.star.basic.service.util.DateUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author wugaungxi
 * @Description
 * @Date 2024/5/7 16:28
 */
@Service
public class BillingOwnerService {

    @Autowired
    private BillingOwnerMapper billingOwnerMapper;

    private static final List<String> OTHER_COMPONENT_LIST = Arrays.asList("flume", "clickhouse", "hbase", "starrocks", "kafka");

    private static final String FLINK_COMPONENT = "flink";
    /**
     * 根据日期和oa查询
     * @param dt
     * @param oa
     * @return
     */
    public List<BillingOwnerEntity> findByDtAndOa(String dt, String oa){
        if(Strings.isEmpty(dt) || Strings.isBlank(oa)){
            return Collections.emptyList();
        }
        return billingOwnerMapper.findByOaAndDt(dt, oa);
    }

    /**
     * 根据日期oa查询flink成本list
     * @param dt
     * @param oa
     * @return
     */
    public List<BillingOwnerPageBO> findFlinkList(String dt, String oa){
        if(Strings.isEmpty(dt)){
            return Collections.emptyList();
        }
        // 当前list
        List<BillingOwnerEntity> currList = billingOwnerMapper.findListByParams(dt, oa, Collections.singletonList(FLINK_COMPONENT));
        // 昨天list
        List<BillingOwnerEntity> yesterdayList = billingOwnerMapper.findListByParams(getLastDay(dt, 1), oa, Collections.singletonList(FLINK_COMPONENT));
        // 上周list
        List<BillingOwnerEntity> lastWeekList = billingOwnerMapper.findListByParams(getLastDay(dt, 7), oa, Collections.singletonList(FLINK_COMPONENT));
        return pack2PageBO(currList, yesterdayList, lastWeekList);
    }

    /**
     * 根据日期oa查询其他成本list
     * @param dt
     * @param oa
     * @return
     */
    public List<BillingOwnerPageBO> findOtherList(String dt, String oa, String component){
        if(Strings.isEmpty(dt)){
            return Collections.emptyList();
        }
        List<String> componentList;
        if(Strings.isEmpty(component)){
            componentList = OTHER_COMPONENT_LIST;
        }else {
            componentList = Collections.singletonList(component);
        }
        // 当前list
        List<BillingOwnerEntity> currList = billingOwnerMapper.findListByParams(dt, oa, componentList);
        // 昨天list
        List<BillingOwnerEntity> yesterdayList = billingOwnerMapper.findListByParams(getLastDay(dt, 1), oa, componentList);
        // 上周list
        List<BillingOwnerEntity> lastWeekList = billingOwnerMapper.findListByParams(getLastDay(dt, 7), oa, componentList);
        return pack2PageBO(currList, yesterdayList, lastWeekList);
    }

    /**
     * 封装分页查询数据
     * @param currList
     * @param yesterdayList
     * @param lastWeekList
     * @return
     */
    private List<BillingOwnerPageBO> pack2PageBO(List<BillingOwnerEntity> currList, List<BillingOwnerEntity> yesterdayList, List<BillingOwnerEntity> lastWeekList){
        Map<String, BillingOwnerEntity> yesterdayMap = yesterdayList.stream()
                .collect(Collectors.toMap(this::getKey, e -> e, (v1, v2) -> v1));
        Map<String, BillingOwnerEntity> lastWeekMap = lastWeekList.stream()
                .collect(Collectors.toMap(this::getKey, e -> e, (v1, v2) -> v1));

        return currList.stream()
                .map(entity -> {
                    BillingOwnerPageBO pageBO = new BillingOwnerPageBO();
                    BeanUtils.copyProperties(entity, pageBO);
                    String key = getKey(entity);
                    BillingOwnerEntity yesterdayData = yesterdayMap.get(key);
                    BillingOwnerEntity lastWeekData = lastWeekMap.get(key);
                    if(yesterdayData != null){
                        pageBO.setUsedTotalDayDiff(NumberUtils.sub(entity.getUsedTotal(), yesterdayData.getUsedTotal()));
                        pageBO.setUsedTotalDayRate(NumberUtils.div(pageBO.getUsedTotalDayDiff(), yesterdayData.getUsedTotal(), 4));
                    }
                    if(lastWeekData != null){
                        pageBO.setUsedTotalWeekDiff(NumberUtils.sub(entity.getUsedTotal(), lastWeekData.getUsedTotal()));
                        pageBO.setUsedTotalWeekRate(NumberUtils.div(pageBO.getUsedTotalWeekDiff(), lastWeekData.getUsedTotal(), 4));
                    }
                    return pageBO;
                }).collect(Collectors.toList());
    }

    private String getLastDay(String date, int day){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(DateUtil.getDate(date, DateUtil.DATE_FORMAT));
        calendar.add(Calendar.DAY_OF_MONTH, -day);
        return DateUtil.getDateStr(calendar.getTime(), DateUtil.DATE_FORMAT);
    }

    private String getKey(BillingOwnerEntity entity){
        return entity.getUserOa() + entity.getComponentsName();
    }
}
